package caseManagerBusiness;

import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yangxianming on 2018/7/26.
 */
public class UpdateCase implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        //初始化返回对象。读取请求
        ReturnJson returnJson = new ReturnJson();
        JSONObject apiJson = GetRequestInputStream.getJson(request);
//        System.out.println(apiJson.toJSONString());

        String set = "";
        //判断是否包换caseName
        if (apiJson.containsKey("caseName")){
            String caseName = apiJson.getString("caseName");
            caseName = caseName.replace("\"", "\\\"");
            caseName = caseName.replace("\'", "\\\'");
            set = set + ", `caseName`=\"" + caseName + "\"";
        }
        //判断是否包换caseName
        if (apiJson.containsKey("describe")){
            String describe = apiJson.getString("describe");
            describe = describe.replace("\"", "\\\"");
            describe = describe.replace("\'", "\\\'");
            set = set + ", `describe`=\"" + describe + "\"";
        }
        //判断projectName是否有
        if (apiJson.containsKey("projectName")){
            set = set + ", `projectName`=\"" + apiJson.getString("projectName") + "\"";
        }
        //判断是否有isjump
        if (apiJson.containsKey("isjump")){
            set = set + ", `isjump`=\"" + apiJson.getString("isjump") + "\"";
        }
        //判断data是否有数据
        if (apiJson.containsKey("data")){
            set = set + ", `data`=\'" + apiJson.getJSONObject("data").toJSONString() + "\'";
        }
        //是否有ipAddress数据
        if (apiJson.containsKey("ipAddress")){
            set = set + ", `ip`=\'" + apiJson.getString("ipAddress") + "\'";
        }
        set = set.substring(1);

        String where = "id=\"" + apiJson.getString("id") + "\"";
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        SqlReturnModel sqlReturnModel = sqlDispatch.update("testcase", set, where);

        returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
        returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        return returnJson.toString();
    }
}
